import { LOCAL_STORAGE_PREFIX } from '@/config'

export const setItem = (name, token) => {
  localStorage.setItem(`${LOCAL_STORAGE_PREFIX}_${name}`, token)
}

export const getItem = (name) => {
  return localStorage.getItem(`${LOCAL_STORAGE_PREFIX}_${name}`) || ''
}

export const removeItem = (name) => {
  return localStorage.removeItem(`${LOCAL_STORAGE_PREFIX}_${name}`)
}

export function parsePage(val, defaultVal) {
  val = parseInt(val)
  return Number.isInteger(val) ? val : defaultVal
}

export function parseQuery(data) {
  const items = {}
  const keys = Object.keys(data)
  for (let index = 0; index < keys.length; index++) {
    const key = keys[index]
    const val = data[key]
    if (val) {
      if (key == 'page' && val == 1) {
        continue
      }

      if (key == 'limit' && val == 10) {
        continue
      }

      items[key] = val
    }
  }
  return items
}

export const transformDuration = (duration, full) => {
  var hours = Math.floor(duration / 3600)
  var minutes = Math.floor((duration - hours * 3600) / 60)
  var seconds = duration % 60

  if (hours || full) {
    hours = hours.toString().padStart(2, '0') + ':'
  } else {
    hours = ''
  }
  minutes = minutes.toString().padStart(2, '0')
  seconds = seconds.toString().padStart(2, '0')

  // 拼接并返回结果
  return hours + minutes + ':' + seconds
}
